package com.dkd.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.*;
import com.dkd.manage.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备管理Service业务层处理
 * 
 * @author guowenjie
 * @date 2025-06-20
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService 
{
    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Autowired
    private VmTypeMapper vmTypeMapper;

    @Autowired
    private NodeMapper nodeMapper;

    @Autowired
    private ChannelMapper channelMapper;


    /**
     * 查询设备管理
     * 
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VendingMachine selectVendingMachineById(Long id)
    {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     * 
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine)
    {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVendingMachine(VendingMachine vendingMachine)
    {
        //1 保存设备信息
        //生成设备编号,设置设备编号
        String innerCode = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode);
        //根据设备型号id,查询设备类型,设置设备容量
        Long vmTypeId = vendingMachine.getVmTypeId();
        VmType vmType = vmTypeMapper.selectVmTypeById(vmTypeId);
        if (vmType != null) {
            vendingMachine.setChannelMaxCapacity(vmType.getChannelMaxCapacity());
        } else{
            return 0;
        }
        //根据点位id,查询点位信息,设置点位地址 商圈类型 区域id 合作商id
        Long nodeId = vendingMachine.getNodeId();
        //查询点位信息
        Node node = nodeMapper.selectNodeById(nodeId);
        if (node != null) {
            vendingMachine.setAddr(node.getAddress());
            vendingMachine.setBusinessType(node.getBusinessType());
            vendingMachine.setRegionId(node.getRegionId());
            vendingMachine.setPartnerId(node.getPartnerId());
        } else {
            return 0;
        }
        //设置设备型号 未投放设备状态 创建时间 修改时间
        vendingMachine.setVmTypeId(vmTypeId);
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);
        //创建时间
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        
        int rs = vendingMachineMapper.insertVendingMachine(vendingMachine);

        //2 保存设备货道信息
        List<Channel> channels = new ArrayList<>();
        //使用for遍历设备类型,行和列,设置货道编号
        for (int r = 1; r <= vmType.getVmRow(); r++) {
            for (int c = 1; c <= vmType.getVmCol(); c++) {
                Channel channel = new Channel();
                String channelCode = r + "-" + c;
                channel.setInnerCode(innerCode);//货道编号
                channel.setVmId(vendingMachine.getId());//售货机id
                channel.setChannelCode(channelCode);//售货机软编号
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());//货道最大容量
                channel.setCreateTime(DateUtils.getNowDate());
                channel.setUpdateTime(DateUtils.getNowDate());
                channels.add(channel);
            }
        }
        //批量插入货道
        channelMapper.insertChannelBatch(channels);


        return rs;
    }

    /**
     * 修改设备管理
     * 
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine)
    {
        //修改相关点位信息
        Node node = nodeMapper.selectNodeById(vendingMachine.getNodeId());
        if (node != null){
            vendingMachine.setPartnerId(node.getPartnerId());
            vendingMachine.setRegionId(node.getRegionId());
            vendingMachine.setAddr(node.getAddress());
        }
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     * 
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids)
    {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     * 
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id)
    {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }
}
